package com.example.composestudy.ui.home

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.composestudy.ui.theme.MyApplicationTheme
import com.example.composestudy.ui.widget.MyTopAppBar

/**
 * 主轴 如何在容器的主轴上放置可组合子项。对于 Column，主轴是垂直轴；对于 Row，主轴则是水平轴。
 * 交叉轴 可组合子项在容器的交叉轴的对齐方式。对于 Column，交叉轴是水平轴；对于 Row，交叉轴则是垂直轴。
 *  Colum:
 *     主轴：verticalArrangement: Arrangement.Vertical = Arrangement.Top,
 *     交叉轴：horizontalAlignment: Alignment.Horizontal = Alignment.Start,
 *
 *  Row:
 *     主轴：horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
 *     交叉轴：verticalAlignment: Alignment.Vertical = Alignment.Top,
 */

@Composable
fun WellnessScreen(
    onBack: () -> Unit,
    wellnessViewModel: WellnessViewModel = viewModel()
) {
    Scaffold(topBar = {
        MyTopAppBar(title = "基础知识") {
            onBack.invoke()
        }
    }) { innerPadding ->
        Column(modifier = Modifier.padding(top = innerPadding.calculateTopPadding())) {
            StatefulCounter()
            WellnessTasksList(list = wellnessViewModel.tasks, onCheckedTask = { task, checked ->
                wellnessViewModel.changeTaskChecked(task, checked)
            }, onCloseTask = { task -> wellnessViewModel.remove(task) })
        }
    }
}

@Composable
fun WellnessTaskItem(
    taskName: String,
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    onClose: () -> Unit,
    modifier: Modifier = Modifier
) {
    Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically) {
        Text(
            text = taskName, modifier = Modifier
                .weight(1f)
                .padding(start = 16.dp)
        )
        Checkbox(checked = checked, onCheckedChange = onCheckedChange)
        IconButton(onClick = onClose) {
            Icon(imageVector = Icons.Default.Close, contentDescription = null)
        }
    }
}

@Composable
fun StatelessCounter(count: Int, onIncrement: () -> Unit, modifier: Modifier = Modifier) {
    Column(modifier = modifier.padding(16.dp)) {
        if (count > 0) {
            Text("You've had $count glasses.")
        }
        Button(onClick = onIncrement, Modifier.padding(top = 8.dp), enabled = count < 10) {
            Text("Add one")
        }
    }
}

@Composable
fun StatefulCounter() {
    var count by rememberSaveable { mutableIntStateOf(0) }
    StatelessCounter(count = count, onIncrement = { count++ })
}

@Preview
@Composable
fun WellnessScreenPreview() {
    MyApplicationTheme {
        Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
            WellnessScreen(onBack = {})
        }
    }
}